package com.espirit.eap.sa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Transient;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import com.espirit.eap.manager.EnumUserType;
import com.googlecode.cswish.model.Saas;


@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@TypeDefs({
	@TypeDef(
		name="FieldPermissionType" ,typeClass=EnumUserType.class,
		parameters={@Parameter(name  = "enumClassName" , value  = "com.espirit.eap.sa.FieldPermissionType")}
	),
	@TypeDef(
		name="scopeType" ,typeClass=EnumUserType.class,
		parameters={@Parameter(name  = "enumClassName" , value  = "com.espirit.eap.sa.ScopeType")}
	)
})

public class FieldPermission implements Serializable , Saas{
	private static final long serialVersionUID = -6562797299190806778L;

	private Integer id;
	
	private String fieldName; 
	private String fieldNameLabel;		// the i18n name of fieldName
	
	// at present, the interface supports 3 kinds of permission: none, read, readwrite
	private FieldPermissionType fieldPermissionType;
	
	// Transient field, used by the ftl page, store the max permission range: NONE --> READ --> WRITE --> READWRITE
	// at present, the enable type can be READ or READWRITE, see PrivilegeService.showFieldPrivilege
	private FieldPermissionType enableFieldPermissionType;
	
	// for the read permission scope
	private ScopeType scopeType;
	// for the write permission scope, it's valid only when fieldPermissionType is write, readwrite
	private ScopeType writeScopeType;
	
	@Deprecated
	private String condition;
	
	
	
	
	
	
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
//	@ManyToOne(targetEntity=String.class,fetch=FetchType.EAGER)
	public String getFieldName() {
		return fieldName;
	}
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	@Type(type="FieldPermissionType")
	public FieldPermissionType getFieldPermissionType() {
		return fieldPermissionType;
	}
	public void setFieldPermissionType(FieldPermissionType filePermissionType) {
		this.fieldPermissionType = filePermissionType;
	}
	
	
	/**
	 * UserService builds the field condition according to the scopeType
	 * @return
	 */
	@Deprecated
	@Column(name="fieldCondition")
	public String getCondition() {
		return condition;
	}
	public void setCondition(String condition) {
		this.condition = condition;
	}
	
	@Transient
	public String getFieldNameLabel() {
		return fieldNameLabel;
	}
	public void setFieldNameLabel(String fieldNameLabel) {
		this.fieldNameLabel = fieldNameLabel;
	}
	@Transient
	public FieldPermissionType getEnableFieldPermissionType() {
		return enableFieldPermissionType;
	}
	public void setEnableFieldPermissionType(FieldPermissionType enableFieldPermissionType) {
		this.enableFieldPermissionType = enableFieldPermissionType;
	}
	
	@Type(type="scopeType")
	public ScopeType getScopeType() {
		return scopeType;
	}
	public void setScopeType(ScopeType scopeType) {
		this.scopeType = scopeType;
	}
	
	@Type(type="scopeType")
	public ScopeType getWriteScopeType() {
		return writeScopeType;
	}
	public void setWriteScopeType(ScopeType scopeTypeWrite) {
		this.writeScopeType = scopeTypeWrite;
	}
	
	public FieldPermission copyTo(FieldPermission toFieldPermission) {
		if (toFieldPermission == null) {
			toFieldPermission = new FieldPermission();
		}
		toFieldPermission.setCondition(getCondition());
		toFieldPermission.setFieldName(getFieldName());
		toFieldPermission.setFieldNameLabel(getFieldNameLabel());
		toFieldPermission.setFieldPermissionType(getFieldPermissionType());
		toFieldPermission.setScopeType(getScopeType());
		toFieldPermission.setWriteScopeType(getWriteScopeType());
		return toFieldPermission;
	}
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
}
